package com.example.teamtribe.main.club.manage.feedback.apply;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.teamtribe.R;
import com.example.teamtribe.common.userinfo.UserInfoActivity;
import com.example.teamtribe.database.TeamRequest;
import com.example.teamtribe.database.User;
import com.example.teamtribe.json.JsonTeamRequestReason;
import com.example.teamtribe.utils.Utility;
import com.google.gson.Gson;

import java.util.List;

public class AppListAdapter extends RecyclerView.Adapter<AppListAdapter.ViewHolder> {

    public static final int REQUEST = 1;
    private Context context;
    private List<TeamRequest> requests;
    private Agree agree;

    public AppListAdapter(List<TeamRequest> requests, Agree agree) {
        this.requests = requests;
        this.agree = agree;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        context = parent.getContext();
        View view = LayoutInflater.from(context).inflate(R.layout.item_apply, parent, false);
        final ViewHolder holder = new ViewHolder(view);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getAdapterPosition() - 1;
                Intent intent = new Intent(context, UserInfoActivity.class);
                TeamRequest request = requests.get(position);
                intent.putExtra("request", request);
                ((Activity) context).startActivityForResult(intent, REQUEST);
            }
        });
        view.findViewById(R.id.bt_agree).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getAdapterPosition() - 1;
                agree.agree(position);
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        TeamRequest request = requests.get(position);
        User user = new Gson().fromJson(request.getRequestReason(), JsonTeamRequestReason.class).getUser();
        if (user != null) {
            Glide.with(context).load(Utility.getUri(user.getPortrait())).into(holder.ivHead);
            holder.tvName.setText(user.getName());
            holder.tvAcademy.setText(user.getAcademy());
            holder.tvMajorCla.setText(user.getMajor());
        }
        switch (request.getResult()) {
            case 1:
                holder.btAgree.setEnabled(false);
                holder.btAgree.setBackground(context.getDrawable(R.drawable.button_stroke_unable));
                holder.btAgree.setTextColor(context.getResources().getColor(R.color.unable));
                holder.btAgree.setText("已同意");
                break;
            case 2:
                holder.btAgree.setEnabled(false);
                holder.btAgree.setBackground(context.getDrawable(R.drawable.button_stroke_unable));
                holder.btAgree.setTextColor(context.getResources().getColor(R.color.unable));
                holder.btAgree.setText("已拒绝");
                break;
            default:
                holder.btAgree.setBackground(context.getDrawable(R.drawable.button_stroke));
                holder.btAgree.setTextColor(context.getResources().getColor(R.color.colorAccent));
                holder.btAgree.setText("同意");
                break;
        }
    }

    @Override
    public int getItemCount() {
        return requests.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        ImageView ivHead;
        TextView tvName;
        TextView tvAcademy;
        TextView tvMajorCla;
        Button btAgree;

        public ViewHolder(View itemView) {
            super(itemView);
            ivHead = itemView.findViewById(R.id.iv_head);
            tvName = itemView.findViewById(R.id.tv_name);
            tvAcademy = itemView.findViewById(R.id.tv_academy);
            tvMajorCla = itemView.findViewById(R.id.tv_major_cla);
            btAgree = itemView.findViewById(R.id.bt_agree);
        }
    }

    interface Agree {

        void agree(int requestId);
    }
}
